package net.huashitong.synthetical.question.web;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.JqGrid;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.sys.util.SysUserUtils;
import com.sirdc.modules.utils.ObjectUtils;
import net.huashitong.synthetical.TaskJob.Entity.TaskJob;
import net.huashitong.synthetical.TaskJob.Service.TaskJobService;
import net.huashitong.synthetical.question.entity.Coordinate;
import net.huashitong.synthetical.question.entity.SupervisionTask;
import net.huashitong.synthetical.question.filter.CoordinateFilter;
import net.huashitong.synthetical.question.service.CoordinateService;
import net.huashitong.synthetical.question.service.SupervisionTaskService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.WebUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/qestionReport")
public class QuestionReport extends JsonBaseController {
    @Autowired
    SupervisionTaskService supervisionTaskService;
    @Autowired
    CoordinateService coordinateService;
    @Autowired
    TaskJobService taskJobService;
    @Override
    protected String getView(String s) {
        return "/modules/question/" + s;
    }

    @RequestMapping(method = RequestMethod.GET)
    public String initPage() {

        return getView("query");
    }


    @ResponseBody
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public Message create(@ModelAttribute CoordinateFilter filter) {
        filter.setUserId(SysUserUtils.getUserId());
        Coordinate coordinate=new Coordinate();
        BeanUtils.copyProperties(filter,coordinate);
        coordinate.setAaa001("1");//1 有效。
        coordinateService.save(coordinate);
        return coverMessage("200");
    }
    /**
     * 申请协调
     *
     * @param model
     * @return url
     */
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String showAddView(@ModelAttribute CoordinateFilter filter , Model model) {
        model.addAttribute("coordinate",filter);
        return getView("creatTask");
    }
    //事项处理
    @RequestMapping(value = "/edit1/{id}", method = RequestMethod.GET)
    public String showEditView1(Model model, @PathVariable String id) {
        Coordinate coordinate=coordinateService.getById(id);
        model.addAttribute("coordinate", coordinate);
        //return getView("creatTask1");
        return getView("form1");
    }


    /**
     * 协调事项处理
     * @param coordinate
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/edit1", method = RequestMethod.POST)
    public Message update1(@ModelAttribute CoordinateFilter coordinate) {

        TaskJob task=new TaskJob();
//        SupervisionTask task  = new SupervisionTask();
      /*  task.setTaskName(coordinate.getTaskName());
        task.setTaskContent(coordinate.getTaskContent());
        task.setPlanEndDate( coordinate.getStartDate());
        task.setPlanEndDate(coordinate.getEndDate());
        task.setStartDate(coordinate.getStartDate());*/
        BeanUtils.copyProperties(coordinate,task);

        task.setAaa001("1");
        task.setTaskResult("0");
        task.setRespResult("0");
        task.setSupervisionResult("0");
        task.setIsfeedback("0");
        task.setTasktimes("1");

        taskJobService.save(task);

        Coordinate coor   = coordinateService.getById(coordinate.getSysId());
        if(ObjectUtils.isNotBlank(coor))
        {
            coor.setAaa001("0");
            coordinateService.update(coor);
         }else
        {
          return  coverMessage("500","找不到该协调事项");
        }

    /*    Date d = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String now = df.format(d);
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(d);
        rightNow.add(Calendar.DAY_OF_YEAR,15);
        Date d1=rightNow.getTime();
        String later = df.format(d1);
        supervisionTask.setProjectName(coordinate.getProjectName());
        supervisionTask.setTaskContent(coordinate.getHelpInfo());
        supervisionTask.setResDept(coordinate.getResDept());
        supervisionTask.setStartDate(now);
        supervisionTask.setEndDate(later);
        SupervisionTask supervisionTask1=new SupervisionTask();
        supervisionTask.setRespResult("0");
        supervisionTask.setSupervisionResult("0");
        supervisionTask.setAaa001("1");
        supervisionTask.setIsrebuild("0");
        supervisionTask.setTasktimes("1");
        BeanUtils.copyProperties(supervisionTask,supervisionTask1);
        supervisionTaskService.save(supervisionTask);*/
        return coverMessage("200");
    }
//    @ResponseBody
//    @RequestMapping(value = "/create1", method = RequestMethod.POST)
//    public Message create1(@ModelAttribute SupervisionTaskFilter filter) {
//        Date d = new Date();
//        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
//        String now = df.format(d);
//        Calendar rightNow = Calendar.getInstance();
//        rightNow.setTime(d);
//        rightNow.add(Calendar.DAY_OF_YEAR,15);
//        Date d1=rightNow.getTime();
//        String later = df.format(d1);
//        filter.setPlanStartDate(now);
//        filter.setPlanEndDate(later);
//        filter.setTaskResult("0");
//        filter.setRespResult("0");
//        filter.setSupervisionResult("0");
//        filter.setAaa001("1");
//        filter.setIsrebuild("0");
//        filter.setTasktimes("1");
//        SupervisionTask supervisionTask=new SupervisionTask();
//        BeanUtils.copyProperties(filter,supervisionTask);
//        supervisionTaskService.save(supervisionTask);
//        return coverMessage("200");
//    }
//    /**
//     * 事项处理
//     *
//     * @param model
//     * @return url
//     */
//
//    @RequestMapping(value = "/create1", method = RequestMethod.GET)
//    public String showAddView1(@ModelAttribute SupervisionTaskFilter filter , Model model) {
//        model.addAttribute("supervisionTask",filter);
//        return getView("creatTask1");
//    }
    @RequestMapping("/filter")
    public String filter(@ModelAttribute CoordinateFilter filter) {
        return getView("filter");
    }
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST)
    public JqGrid query(@ModelAttribute CoordinateFilter filter) {
        List<Coordinate> lists = coordinateService.queryCoordinate(filter);
        return coverJqGrid(filter,lists);
    }
    @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
    public String showEditView(Model model, @PathVariable String id) {

        Coordinate coordinate=coordinateService.getById(id);
        model.addAttribute("coordinate", coordinate);
        return getView("form");
    }

    @ResponseBody
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public Message update(@ModelAttribute Coordinate coordinate) {
        coordinateService.update(coordinate);
        return coverMessage("200");
    }
}
